package com.photographysharing.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.photographysharing.entity.Files;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.mapper.FilesMapper;
import com.photographysharing.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;


@Service
@Transactional
public class FileServiceImpl implements FileService {

    @Autowired
    private FilesMapper filesMapper;

    /**
     * 获取项目跟路径
     */
    private final String dir = System.getProperty("user.dir");

    /**
     * 获取ip 获取失败就用127.0.0.1
     */
    private final String IP = "获取失败".equals(getIp()) ? "127.0.0.1" : getIp();

    /**
     * 文件存放地址
     */
    private final String fileAddress = dir + "/files";

    /**
     * 获取springboot的运行端口
     */
    private String port;

    /**
     * 获取本机ip
     */
    public String getIp() {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            return localHost.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return "获取失败";
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        //获取名字
        String originalFilename = file.getOriginalFilename();

        //获取文件的类型
        String type = FileUtil.extName(originalFilename);

        //获取长度
        long size = file.getSize();

        //判断 当前存储的地址 存不存在 不存在就创建
        File uploadPathFile = new File(fileAddress);
        if (!uploadPathFile.exists()) {
            uploadPathFile.mkdirs();
        }

        // UUID全称通用唯一识别码
        String uuid = IdUtil.simpleUUID();

        try {
            //存储到磁盘上
            File uploadFile = new File(fileAddress + "/" + uuid + StrUtil.DOT + type);
            file.transferTo(uploadFile);

            //数据存储到数据库
            Files fileDocument = new Files();
            fileDocument.setId(null);
            fileDocument.setName(originalFilename);
            fileDocument.setType(type);
            fileDocument.setUuid(uuid);
            fileDocument.setSize(size);
            fileDocument.setCreateTime(new Date());
            filesMapper.insert(fileDocument);

            return uuid;
        } catch (Exception e) {
            throw new ServiceException("上传失败!");
        }
    }

    @Override
    public String[] getFileAddress(String uuid) {
        String type = filesMapper.getTypeByUUID(uuid);

        if ("".equals(type) || type == null) {
            throw new ServiceException("文件不存在!");
        }
        String[] strings = new String[5];

        strings[0] = fileAddress + "/" + uuid + StrUtil.DOT;
        strings[1] = type;

        // 返回uuid 对应的文件地址 和 文件类型
        return strings;
    }
}
